package br.com.ftt.database;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.fsa.aplication.Model.bean.NotaFiscal;
import br.com.ftt.exception.ConectionException;
import br.com.ftt.util.HibernateUtil;
import br.com.ftt.util.Util;

public class NotaFiscalDAO extends DaoGenericoImpl<NotaFiscal>
{

	public Boolean findLogisticaByCodigo(int codNota, long codgistica)
			throws ConectionException
	{
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction transaction = session.beginTransaction();
			transaction.begin();
			Criteria criteria = session.createCriteria(NotaFiscal.class).add(Restrictions.eq("nNF", codNota));
			criteria = criteria.createCriteria("logistica");
			criteria = criteria.add(Restrictions.eq("codLog", codgistica));
		
			transaction.commit();

			if (criteria.list().size() == 0)
			{
				return false;
			}
			else
			{
				return true;
			}

		}
		catch (HibernateException e)
		{
			Util.gravaLog(e.getMessage());
			throw new ConectionException(e.getMessage());

		}
		catch (ConectionException e)
		{
			Util.gravaLog(e.getMessage());
			throw e;
		}
		finally
		{
			if( session.isOpen())
			{
				session.close();
			}
			
		}

	}

}
